﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Exp
{
    public partial class Exp : Form
    {
        private int childFormNumber = 0;

        public Exp()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "窗口 " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            openFileDialog.Filter = "网页文件(*.html)|*.html|网页文件(*.htm)|*.htm|文本文件(*.txt)|*.txt|高级文本文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                switch(System.IO.Path.GetExtension(openFileDialog.FileName))
                {
                    case ".html":
                        ExpW expw = new ExpW(openFileDialog.FileName);
                        expw.MdiParent = this;
                        expw.Show();
                        break;
                    case ".png":
                    case ".bmp":
                        ImageExpW iExpW = new ImageExpW(openFileDialog.FileName);
                        iExpW.MdiParent = this;
                        iExpW.Show();
                        break;
                    case ".rtf":
                        RichTextWindow rtw
                       = new RichTextWindow(openFileDialog.FileName);
                        rtw.MdiParent = this;
                        rtw.Show();
                        break;
                    case ".txt":
                        TextWindow tw = new TextWindow(openFileDialog.FileName);
                        tw.MdiParent = this;
                        tw.Show();
                        break;
                    default:
                        TextWindow tws = new TextWindow(openFileDialog.FileName);
                        tws.MdiParent = this;
                        tws.Show();
                        break;

                }
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void 浏览器窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpW exp = new ExpW();
            exp.MdiParent = this;
            exp.Show();
        }

        private void 图片窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageExpW iew = new ImageExpW();
            iew.MdiParent = this;
            iew.Show();
        }

        private void 文本窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextWindow tw = new TextWindow();
            tw.MdiParent = this;
            tw.Show();
        }

        private void 高级文本窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RichTextWindow rtw = new RichTextWindow();
            rtw.Parent = this;
            rtw.Show();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolWindow TW = new ToolWindow();
            TW.ShowDialog(this);
        }

        private void Exp_FormClosed(object sender, FormClosedEventArgs e)
        {
            StreamWriter stream = new StreamWriter("config.txt");
            stream.WriteLine(Setting.AutoImage);
            stream.WriteLine(Setting.HtmlSite);
            stream.Close();
        }
    }
    public class WindowTemplate : Form
    {
        public WindowTemplate(string Title)
        {
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new Size(100, 100);
            this.Text = Title;
        }
    }

    public class ExpW : WindowTemplate
    {
        WebBrowser wBrowser = new WebBrowser();
        public ExpW(string WebSiteName = "") : base("内置浏览器")
        {
            wBrowser.Size = new Size(100, 100);
            if(WebSiteName != "")
            {
                wBrowser.Url = new Uri(WebSiteName);
            }
            else
            {
                if(Setting.HtmlSite != null || Setting.HtmlSite != "")
                {
                    wBrowser.Url = new Uri(Setting.HtmlSite);
                }
                else
                {
                    wBrowser.Url = new Uri("https://www.baidu.com");
                }
            }
            wBrowser.NewWindow += WBrowser_NewWindow;
            //----------------------------
            this.Controls.Add(wBrowser);
            this.SizeChanged += new EventHandler((sender, e) => { wBrowser.Size = this.Size; });
        }

        private void WBrowser_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            string u = wBrowser.StatusText;
            wBrowser
                .
                Url
                =
                new
                Uri
                (
                    u
                )
                ;
        }
    }

    public class ImageExpW : WindowTemplate
    {
        Button Select = new Button();
        PictureBox Image = new PictureBox();
        public ImageExpW(string ImagePath = "") : base("图片浏览器")
        {
            if(ImagePath != "")
            {
                Image.Image = System.Drawing.Image.FromFile(ImagePath);
            }
            else
            {
                if(Setting.AutoImage != null || Setting.AutoImage != "")
                {
                    Image.Image = System.Drawing.Image.FromFile(Setting.AutoImage);
                }
                else
                {
                    
                }
            }
            Image.SizeMode = PictureBoxSizeMode.StretchImage;
            Select.Size = new Size(50, Height);
            Select.Text = "浏览文件";
            Select.Click += (sender, e) =>
            {
                try
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "图片文件(*.jpg)|*.jpg|无损图片(*.png)|*.png|位图文件(*.bmp)|*.bmp|所有文件(*.*)|*.*";
                    var result = ofd.ShowDialog();
                    switch (result)
                    {
                        case DialogResult.OK:
                            Image.Image = System.Drawing.Image.FromFile(ofd.FileName);
                            return;
                        case DialogResult.None:
                        case DialogResult.Cancel:
                        case DialogResult.Abort:
                        case DialogResult.Retry:
                        case DialogResult.Ignore:
                        case DialogResult.Yes:
                        case DialogResult.No:
                        default:
                            return;
                    }
                }
                catch
                {
                    return;
                }
            };
            Image.Location = new Point(50, 0);
            Image.Size = new Size(Width - 50, Height);

            //-------------------
            this.Controls.AddRange(new Control[2] { Select, Image });
            SizeChanged += (sender, e) =>
            {
                Select.Size = new Size(50, Height);
                Image.Location = new Point(50, 0);
                Image.Size = new Size(Width - 50, Height);
            };
        }
    }
    public class TextWindow : WindowTemplate
    {
        TextBox tb = new TextBox();
        public TextWindow(string path = "") : base("文本窗口")
        {
            tb.ScrollBars = ScrollBars.Vertical;
            ReadFile(path);
            tb.Size = Size;
            tb.Multiline = true;
            Controls.Add(tb);
            SizeChanged += new EventHandler((sender, e) => { tb.Size = Size; });
        }

        private async void ReadFile(string path)
        {
            if (path != "")
            {
                StreamReader stream = new StreamReader(path, Encoding.Unicode);
                while (!stream.EndOfStream)
                {
                    tb.AppendText(stream.ReadLine());
                }
            }
        }
    }
    public class RichTextWindow : WindowTemplate
    {
        RichTextBox rtb = new RichTextBox();
        public RichTextWindow(string FileName = "") : base("文本窗口")
        {
            if(FileName != "")
            {
                rtb.LoadFile(FileName);
            }
            rtb.Size = Size;
            rtb.Multiline = true;
            Controls.Add(rtb);
            SizeChanged += new EventHandler((sender, e) => { rtb.Size = Size; });
        }
    }
}
